<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin_model extends CI_Model {
    
    public $tab_auth = 'hpg_user_auth';
    
    public $tab_apply = 'hpg_project_apply';
    
    public $tab_project = 'hpg_project';
    
    public function getApplyList($page=1, $limit=20){
        $start = ($page-1)*$limit;
        $countsql = 'select count(*)';
        $sql = ' from '.$this->tab_apply.' as apply 
            LEFT JOIN '.$this->tab_auth.' as auth on apply.uid=auth.uid 
            LEFT JOIN '.$this->tab_project.' as project on apply.project_id=project.id 
            order by apply.id desc limit 0,20';
        $count = $this->db->query($countsql.$sql)->result_array();
        $selectsql = 'select apply.id as apply_id,apply.project_id,apply.type,project_name,auth.*';
        $list = $this->db->query($selectsql.$sql)->result_array();
        return [intval($count[0]), $list ? $list : []];
    }
    
    public function updateApply($map, $data){
        if( !$map || !$data ){
            return false;
        }
        $res = $this->db->update($this->tab_apply, $data, $map);
        return $res;
    }
    
        
    public function getAuthList($page=1, $limit=20){
        $start = ($page-1)*$limit;
        $query = $this->db;
        $count = $query->count_all_results($this->tab_auth, false);
        $list = $query->select('*')->limit($limit, $start)->order_by('id', 'desc')->get_array();
        return [intval($count), $list ? $list : []];
    }
    
    public function updateAuth($map, $data){
        if( !$map || !$data ){
            return false;
        }
        $res = $this->db->update($this->tab_auth, $data, $map);
        return $res;
    }
}